package com.apexedu.portal.service.impl;

import com.apexedu.biz.Const;
import com.apexedu.framework.service.impl.AbstractBaseServiceSupport;
import com.apexedu.framework.util.DBUtil;
import com.apexedu.framework.util.Pager;
import com.apexedu.framework.util.SeqFactory;
import com.apexedu.identity.action.IdLoginAction;
import com.apexedu.portal.action.FrontdesknewsbaseAction;
import com.apexedu.portal.action.PortalNavLinkAction;
import com.apexedu.portal.entity.TFrontdeskNews;
import com.apexedu.portal.entity.TNavLink;
import com.apexedu.sys.util.BusinessLogUtil;
import com.apexedu.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * Created by liwang on 2017/7/4.
 */
@Service("portalNavLinkService")
public class PortalNavLinkServiceImpl extends AbstractBaseServiceSupport {
    private static final Logger logger = Logger.getLogger(PortalNavLinkServiceImpl.class);

    public List<?> list(Object obj, Pager pager) throws Exception {
        long c= DBUtil.count("select count(*) as c from t_nav_link t ");
        pager.setTotalRows(c);
        String sql="select wid, name, href, sort, state, remark from t_nav_link t where 1=1 order by sort asc";
        List<TNavLink> list = DBUtil.queryPageBeanList(pager,sql, TNavLink.class);
        return list;
    }

    public List<TNavLink> list(){
        String sql="select wid, name, href, sort, state, remark from t_nav_link t where t.state ='1' order by sort asc";
        List<TNavLink> list = DBUtil.queryAllBeanList(sql, TNavLink.class);
        return list;
    }
    @Override
    public void saveOrUpdate(Object myaction) throws Exception {
        PortalNavLinkAction action = (PortalNavLinkAction) myaction;
        TNavLink tNavLink = action.getNavLink();
        if (tNavLink != null) {
            if (StringUtil.isNotBlank(tNavLink.getWid())) {
                baseDao.updateNotNull(tNavLink);
                BusinessLogUtil.log(action.getUserloginid(), Const.CZDX_T_NAV_LINK, Const.LOG_ACTION_UPDATE,
                        IdLoginAction.getIpAddr(action.getRequest()), "修改导航链接");
            } else {
                tNavLink.setWid(UUID.randomUUID().toString());
                baseDao.save(tNavLink);
                BusinessLogUtil.log(action.getUserloginid(), Const.CZDX_T_NAV_LINK, Const.LOG_ACTION_SAVE,
                        IdLoginAction.getIpAddr(action.getRequest()),"新增导航链接");
            }
        }
    }

    @Override
    public void load(Object myaction) throws Exception {
        PortalNavLinkAction action = (PortalNavLinkAction) myaction;
        String wid = action.getWid();
        TNavLink tNavLink = DBUtil.queryBean("select * from T_NAV_LINK t where t.wid = ?", TNavLink.class, wid);
        action.setNavLink(tNavLink);
    }

    @Override
    public boolean remove(Object myaction) throws Exception {
        PortalNavLinkAction action = (PortalNavLinkAction) myaction;
        String wid = action.getWid();
        if (StringUtils.isNotBlank(wid)) {
            DBUtil.executeSQL("delete from T_NAV_LINK where wid = ?", wid);
        }
        return false;
    }
}
